import java.util.Queue;

/**
 * 用单链表来实现queue
 */
public class MyQueue {
    private LinkNode head = null;
    private LinkNode tail = null;
    private int length = 0;

    private class LinkNode{
        private int val;
        private LinkNode next;

        public LinkNode(int val) {
            this.val = val;
        }
    }


    public MyQueue() {}

    //入队
    public int offer(int data){
        LinkNode node = new LinkNode(data);
        if(empty()){
            head = node;
            tail = node;
            length++;
            return data;
        }
        //尾部插入
        tail.next = node;
        tail = node;
        length++;
        return data;
    }

    //出队
    public int poll(){
        if(empty()){
            System.out.println("队列已经空！");
            return -1;
        }
        int res = head.val;
        //只有一个节点
        if(head.next == null){
            head = null;
            tail = null;
            length--;
            return res;
        }
        head = head.next;
        length--;
        return res;
    }

    //获取队头元素
    public int peek(){
        if(empty()){
            System.out.println("队列是空的！");
        }
        return head.val;
    }

    public int getLength(){
        return length;
    }

    public boolean empty(){
        return length == 0;
    }
}
